package student.aistar.dao.impl;

import org.apache.ibatis.session.SqlSession;
import student.aistar.Util.MybatisUtil;
import student.aistar.dao.ITVideoDao;
import student.aistar.model.entity.TVideo;

import java.util.List;

public class TVideoDaoImpl implements ITVideoDao {
    @Override
    public TVideo getById(Integer id) {
        SqlSession session = MybatisUtil.getSqlSession();
        TVideo tVideo = session.selectOne("tvideoMapper.getById", id);
        return tVideo;
    }

    @Override
    public List<TVideo> findAll() {
        SqlSession session = MybatisUtil.getSqlSession();
        List<TVideo> list = session.selectList("tvideoMapper.get");
        return list;
    }

    @Override
    public int save(TVideo tVideo) {
        SqlSession session = null;
        int count = 0;
        try {
            session = MybatisUtil.getSqlSession();
            count = session.insert("tvideoMapper.save", tVideo);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return count;
    }

    @Override
    public int update(TVideo video) {
        SqlSession session = MybatisUtil.getSqlSession();
        int affectRos = session.update("tvideoMapper.update", video);
        session.commit();
        return affectRos;
    }

    @Override
    public int delById(Integer id) {
        return 0;
    }
}
